
//  SystemInfoView.m
//  easyTaxi
//
//  Created by ellenpage on 12-12-20.
//  Copyright (c) 2012年 ellenpage. All rights reserved.
//

#import "SystemInfoView.h"
#import "Constant.h"
#import "PassValueClass.h"
//#import "SysViewObject.h"
#import "RequestCMD.h"
#import "SocketObject.h"

bool isVoiceOff;

@interface SystemInfoView ()<UITableViewDataSource,UITableViewDelegate>

- (IBAction)hiddenSelf;

@property (weak, nonatomic) IBOutlet UIButton *voiceBtn;
@property (weak, nonatomic) IBOutlet UILabel *currentCityLbl;
@property (weak, nonatomic) IBOutlet UIView *cityView_Small;
@property (weak, nonatomic) IBOutlet UIView *cityView;

- (IBAction)showCityView;
- (IBAction)hideCityView;
- (IBAction)callLocalPhone;
- (IBAction)closeOrOpenVoice;

@end

@implementation SystemInfoView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"SystemInfoView" owner:self options:nil] objectAtIndex:0];
        [self initialData];
    }
    return self;
}


- (void)initialData
{
    _cityView.backgroundColor = [[UIColor darkTextColor] colorWithAlphaComponent:0.65];
    [_cityView_Small.layer setCornerRadius:10];
    [_cityView_Small.layer setBorderColor:[[UIColor whiteColor] CGColor]];//描边颜色
    [_cityView_Small.layer setBorderWidth:2.0];//描边粗细
    [self addSubview:_cityView];
    _cityView.hidden = YES;
    
//    bool myVoice = [[NSUserDefaults standardUserDefaults] objectForKey:@"isVoiceOff"];
//
//    if (myVoice)
//    {
//        [_synTheSizeControl setVolume:0];
//        [_voiceBtn setImage:[UIImage imageNamed:@"sound_off0.png"] forState:UIControlStateNormal];
//        [_voiceBtn setImage:[UIImage imageNamed:@"sound_off1.png"] forState:UIControlStateHighlighted];
//    }
//    else
//    {
//        [_synTheSizeControl setVolume:60];
//        [_voiceBtn setImage:[UIImage imageNamed:@"sound_on0.png"] forState:UIControlStateNormal];
//        [_voiceBtn setImage:[UIImage imageNamed:@"sound_on1.png"] forState:UIControlStateHighlighted];
//    }
//
//    isVoiceOff = [[NSUserDefaults standardUserDefaults] objectForKey:@"isVoiceOff"];
    
    NSString *abc = [[NSString alloc] initWithFormat:@"server_url=%@,appid=%@",kXunFeiUrl,kXunFeiKey];
    
    if (!_synTheSizeControl)
    {
        _synTheSizeControl = [[IFlySynthesizerControl alloc] initWithOrigin:kWidgetOrigin initParam:abc];
        _synTheSizeControl.delegate = self;
    }
    
    _mTBView.delegate   = self;
    _mTBView.dataSource = self;
}


- (IBAction)hiddenSelf
{
    self.hidden = YES;
}
- (IBAction)showCityView
{
    _cityView.hidden = NO;
//    NSLog(@"%@",[PassValueClass sharedIntance].cityStr);
    _currentCityLbl.text = [PassValueClass sharedIntance].cityStr;
}

- (IBAction)hideCityView
{
    _cityView.hidden = YES;
}

- (IBAction)callLocalPhone
{
    NSString *phoneStr = [PassValueClass sharedIntance].callNumber;
    if (phoneStr != nil)
    {
        UIWebView*callWebview =[[UIWebView alloc] init];
        NSString *str = [NSString stringWithFormat:@"tel:%@",phoneStr];
        NSURL *telURL =[NSURL URLWithString:str];// 貌似tel:// 或者 tel: 都行
        [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
        [self addSubview:callWebview];   
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"对不起,您的城市未开通电话叫车服务" message:nil delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [alert show];
    }

    [YouMengEvent YouMengEvent:kCallTaxiLocalCity_YouMeng];
}

- (IBAction)closeOrOpenVoice
{
    if (isVoiceOff == NO)
    {
        isVoiceOff = YES;
        [_synTheSizeControl setVolume:0];
        [_voiceBtn setImage:[UIImage imageNamed:@"sound_off0.png"] forState:UIControlStateNormal];
        [_voiceBtn setImage:[UIImage imageNamed:@"sound_off1.png"] forState:UIControlStateHighlighted];
        
        [YouMengEvent YouMengEvent:kCloseVoice_YouMeng];
    }
    else
    {
        isVoiceOff = NO;
        [_synTheSizeControl setVolume:60];
        [_voiceBtn setImage:[UIImage imageNamed:@"sound_on0.png"] forState:UIControlStateNormal];
        [_voiceBtn setImage:[UIImage imageNamed:@"sound_on1.png"] forState:UIControlStateHighlighted];
    }
}


- (void)synthesizeVoice:(NSString *)string//文字转语音
{
    [_synTheSizeControl setText:string params:nil];
    [_synTheSizeControl start];
}


// get the player buffer progress
// 获取播放器缓冲进度
- (void)onSynthesizerBufferProgress:(float)bufferProgress
{
    NSLog(@"the playing buffer :%f",bufferProgress);
}

// get the player progress
// 获取播放器的播放进度
- (void)onSynthesizerPlayProgress:(float)playProgress
{
    NSLog(@"the playing progress :%f",playProgress);
}


#pragma mark - TBView Delegate - TBView代理

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 23;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [_dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }

    NSString *cellStr = [[_dataArray objectAtIndex:indexPath.row] objectForKey:kName];
    cell.textLabel.textAlignment = UITextAlignmentCenter;
    cell.textLabel.text = cellStr;
    
    return cell;
}

#pragma mark - TableView Delegate   - 结构
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *lonStr  = [[_dataArray objectAtIndex:indexPath.row] objectForKey:kLongitude];
    NSString *latStr  = [[_dataArray objectAtIndex:indexPath.row] objectForKey:kLatitude];
    NSString *cityStr = [[_dataArray objectAtIndex:indexPath.row] objectForKey:kCity];
    NSString *areacodeStr  = [[_dataArray objectAtIndex:indexPath.row] objectForKey:kAreaCode];

    [PassValueClass sharedIntance].latStr_TiYanMoShi   = latStr;
    [PassValueClass sharedIntance].lngStr_TiYanMoShi   = lonStr;
    [PassValueClass sharedIntance].areaCode_TiYanMoShi = areacodeStr;
    [PassValueClass sharedIntance].cityStr_TiYanMoShi  = cityStr;

    [PassValueClass sharedIntance].isTiYanMoShi        = YES;
        
    [[NSNotificationCenter defaultCenter] postNotificationName:kChangeTiYanCityNoti object:nil];
    
    [self getNearDrivers];

    self.hidden = YES;
}

#pragma mark - GetNearDrivers - 获取周围司机

- (void)getNearDrivers
{
    NSString *string1011          = [RequestCMD requestCMD:kNearDrivers];
    NSMutableData *dataToSend1011 = [RequestCMD getWantedData:string1011];
    
    [[SocketObject sharedInstance].gcdSocket writeData:dataToSend1011 withTimeout:-1 tag:kNearDrivers];
}

#pragma mark - GoBackCity - 返回当前城市

- (IBAction)goBackCity
{
    [PassValueClass sharedIntance].isTiYanMoShi = NO;
   
    [self getNearDrivers];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kChangeTiYanCityNoti object:nil];

    self.hidden = YES;
}

@end
